<template>
  <div class="role">
    管理员列表
    <el-divider></el-divider>
    <div class="search">
      <p>
        <el-input v-model="searchParams.name" placeholder="请输入姓名"></el-input>
      </p>
      <p>
        <el-input v-model="searchParams.tel" placeholder="请输入电话"></el-input>
      </p>
      <p>
        <el-select v-model="searchParams.type" >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </p>
      <p>
        <el-button type="primary" @click="userAdd">新增</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
      </p>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="m_id" label="编号"></el-table-column>
      <el-table-column prop="m_name" label="姓名"></el-table-column>
      <el-table-column prop="u_tel" label="手机号"></el-table-column>
      <el-table-column prop="m_role" label="类别">
        <template slot-scope="scope">
          {{ scope.row.m_role === 1 ? '超级管理员' : '普通管理员' }}
        </template>
      </el-table-column>
      <el-table-column prop="u_lock" label="账号状态">
        <template slot-scope="scope">
          <span style="color: blue;">{{ scope.row.u_lock === 0 ? '正常' : '锁定' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="u_lock" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope)">编辑</el-button>
          <!-- <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pages.currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pages.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <add-user></add-user>
  </div>
</template>
<script>
import { getRoleList } from '@/api/role'
import addUser from './addUser.vue'
export default {
  name: '',
  components: {
    addUser
  },
  data () {
    return {
      searchParams: {
        name: null,
        tel: null,
        type: null
      },
      options: [
        {
          label: '全部',
          value: null
        },
        {
          label: '超级管理员',
          value: '1'
        },
        {
          label: '普通管理员',
          value: '0'
        }
      ],
      // 给列表 展示列表数据
      tableData: [],
      pages: {
        currentPage: 1,
        pageSize: 10
      },
      // 给分页赋值 总数
      total: 0
    }
  },
  watch : {
    '$store.state.updateSuccess' (val) {
      if (val) {
        this.search()
      }
    }
  },
  mounted () {
    this.getRoleListData()
  },
  methods: {
    getRoleListData () {
      getRoleList({...this.pages, ...this.searchParams}).then(res => {
        const { count, list } = res
        this.tableData = list
        this.total = count
      })
    },
    // 新增用户
    userAdd () {
      // this.$store.state.role.isShow = true
      // 参数列表
      this.$store.commit('updateRole', {
        isShow: true
      })
    },
    // 搜索
    search () {
      this.pages.currentPage = 1
      this.getRoleListData()
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.getRoleListData()
    },
    // 页数改变
    handleCurrentChange (val) {
      this.pages.currentPage = val
      this.getRoleListData()
    },
    handleEdit (scope) {
      // 获取store
      this.$store.commit('updateRole', {
        isShow: true,
        userInfo: scope.row
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 10px 0;
}
.search {
  display: flex;
  p {
    margin: 0;
    padding: 0;
    margin-right:  10px;
  }
}
</style>
